<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property integer $user_id
 * @property string $user_name
 * @property string $user_password
 * @property string $user_salt
 * @property string $user_group
 * @property int $user_created
 * @property int $user_lastlogin
 * @property string $user_remark
 */
class User extends CActiveRecord
{
	const USER_SUPER = 0;		//超级管理员
	const USER_ADMIN = 1;		//管理员
	const USER_FORBID = 2;		//禁用用户
	const USER_NORMAL = 3;		//普通用户	
	
	public $salt;
	public $user_repassword;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_name, user_password, user_group, user_remark', 'required'),
			array('user_name', 'length', 'max'=>24),
			array('user_password', 'length', 'max'=>50),
			array('user_salt', 'length', 'max'=>100),
			array('user_remark', 'length', 'max'=>500),
			array('user_group', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user_id, user_name, user_password, user_salt, user_group, user_remark', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'news' => array(self::HAS_MANY, 'Article', 'user_id'),
			'profile'	=>	array(self::HAS_ONE,'Profile','pro_id'),
			'media' => array(self::HAS_MANY, 'Category','user_id'),
// 			'log'	=> array(self::HAS_MANY, 'Log', 'user_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => Yii::t('user','ID'),
			'user_name' => Yii::t('user','UserName'),
			'user_password' => Yii::t('user','Password'),
			'user_repassword' => Yii::t('user', 'Repeat Password'),
			'user_salt' => Yii::t('user','Salt'),
			'user_group' => Yii::t('user','Group'),
			'user_remark' => Yii::t('user','Remark'),
			'user_create' => Yii::t('user', 'Create'),
			'user_lastlogin' => Yii::t('user', 'Last Login'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_password',$this->user_password,true);
		$criteria->compare('user_salt',$this->user_salt,true);
		$criteria->compare('user_group',$this->user_group,true);
		$criteria->compare('user_remark',$this->user_remark,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function validatePassword($password)
	{
		return $this->hashPassword($password,$this->user_salt) === $this->user_password;
	}
	
	public function hashPassword($password,$salt){
		return md5($salt.$password);
	}
	
	public function getUserById($id)
	{
		return self::model()->findByPk($id);
	}
	
	public function getUsersByGroup($group)
	{
		$result = array();
		
		$users = User::model()->findAll(array(
			'condition' => 'user_group = :user_group',
			'params' => array(
				':user_group' => $group
			)
		));
		
		foreach ($users as $user)
		{
			$result[] = $user->user_name;
		}
		
		return $result;
		
	}
	
	
	
	public function getUserGroupName($id)
	{
		switch ($id)
		{
			case self::USER_SUPER:
				return Yii::t('user',"Super");
			case self::USER_ADMIN:
				return Yii::t('user', 'Admin');
			case self::USER_FORBID:
				return Yii::t('user',"Forbid");
			case self::USER_NORMAL:
				return Yii::t('user', "Normal");
		}
	}
	
	public function getUserGroupArray()
	{
		return array(
			self::USER_ADMIN=>self::getUserGroupName(self::USER_ADMIN),
			self::USER_NORMAL=>self::getUserGroupName(self::USER_NORMAL),
			self::USER_SUPER=>self::getUserGroupName(self::USER_SUPER),
			self::USER_FORBID=>self::getUserGroupName(self::USER_FORBID),
		);
	}
	
	public function generateSalt()
	{
		$salt = md5($this->user_name.$this->user_created);
		$salt = substr($salt, 5, 5);
		return $salt;
	}
	
	public function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->user_created = time();
				$this->user_lastlogin = $this->user_created;
				$this->user_salt = $this->generateSalt();
				$this->user_password = $this->hashPassword($this->user_password, $this->user_salt);
			}
			else
			{
				$this->user_lastlogin = time();	
			}
			return true;	
		}
		else
			return false;
	}
}